package com.common.utils.nrmm;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.common.utils.http.HttpUtils;
import com.model.dto.common.RecordLoginDTO;
import com.model.dto.common.TerminalRecordAuthAddDTO;
import com.model.dto.common.TerminalRecordGetIdentifierByIdDTO;
import com.model.dto.common.VehicleRecordAddDTO;
import com.model.vo.common.VeccRequestVO;
import com.model.vo.common.VeccReturnResultVO;
import com.model.vo.common.VeccReturnVO;
import lombok.extern.slf4j.Slf4j;
import lombok.val;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 *
 * @Description
 * @Author tm
 * @Date 2023-09-13 14:04
 **/
@Slf4j
public class NrmmUtils {
    private static String urls = "https://fdlpfjk.vecc.org.cn/nrmm-api/system/vehiclerecord/outside/add";
    /**
     * 登录
     */
    public static VeccReturnVO recordLogin(String userName, String password) {
        String url = "https://fdlpfjk.vecc.org.cn/nrmm-api/record/login";
        RecordLoginDTO dto = new RecordLoginDTO(userName,password);
        String headers = "";
        String content = JSON.toJSONString(dto);
        String result = HttpUtils.sendPostHeader(url, content,"");
        log.info("返回结果：{}",result);
        VeccReturnVO vo = new VeccReturnVO();
        JSONObject jsonObject = JSONObject.parseObject(result);
        vo.setSuccess(jsonObject.getBoolean("success"));
        vo.setMessage(jsonObject.getString("message"));
        vo.setCode(jsonObject.getInteger("code"));
        if(vo.getCode() == 200){
            JSONObject resultJsonObject = jsonObject.getJSONObject("result");
            VeccReturnResultVO resultVo = new VeccReturnResultVO();
            resultVo.setToken(resultJsonObject.getString("token"));
            vo.setResultVO(resultVo);
        }
        VeccRequestVO requestVO = new VeccRequestVO();
        requestVO.setRequestUrl(url);
        requestVO.setContent(content);
        requestVO.setResult(result);
        vo.setRequestVO(requestVO);
        return vo;
    }

    /**
     * 机械信息备案申请
     */
    public static void vehicleRecordAdd(){
        VehicleRecordAddDTO vehicleRecordAddDTO = new VehicleRecordAddDTO("1VB210063P4230403","TJ20-2",4,0,"","",0);
        String headers="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MDgzOTU1ODMsInVzZXJuYW1lIjoiZ2c2Nm85cWdnbWhnIn0.7JU1QLifO6RUt4e34UxpP2ZzYV8u-nKTPr1i7ZdjKNM";
        System.out.println(JSON.toJSONString(vehicleRecordAddDTO));
        String result = HttpUtils.sendPostHeader(urls, JSON.toJSONString(vehicleRecordAddDTO), headers);
        log.info("返回结果：{}",result);
    }

    /**
     * 终端型号授权备案申请
     */
    public static VeccReturnVO terminalRecordAuthAdd(TerminalRecordAuthAddDTO terminalRecordAuthAddDTO,String token){
        VeccReturnVO vo = new VeccReturnVO();
        String url = "https://fdlpfjk.vecc.org.cn/nrmm-api/system/terminalrecord/outside/auth/add";
        String content = JSON.toJSONString(terminalRecordAuthAddDTO);
        String result = HttpUtils.sendPostHeader(url,content,token);
        JSONObject jsonObject = JSONObject.parseObject(result);
        vo.setSuccess(jsonObject.getBoolean("success"));
        vo.setMessage(jsonObject.getString("message"));
        vo.setCode(jsonObject.getInteger("code"));
        if(vo.getCode() == 200){
            JSONObject resultJsonObject = jsonObject.getJSONObject("result");
            VeccReturnResultVO resultVo = new VeccReturnResultVO();
            resultVo.setRequestID(resultJsonObject.getString("requestID"));
            vo.setResultVO(resultVo);
        }
        VeccRequestVO requestVO = new VeccRequestVO();
        requestVO.setRequestUrl(url);
        requestVO.setContent(content);
        requestVO.setResult(result);
        vo.setRequestVO(requestVO);
        return vo;
    }


    /**
     * 终端型号授权备案结果查询
     */
    public static VeccReturnVO terminalRecordGetIdentifierById(TerminalRecordGetIdentifierByIdDTO terminalRecordGetIdentifierByIdDTO, String token){
        VeccReturnVO vo = new VeccReturnVO();
        String url = "https://fdlpfjk.vecc.org.cn/nrmm-api/system/terminalrecord/outside/getIdentifierById";
        String content = JSON.toJSONString(terminalRecordGetIdentifierByIdDTO);
        String result = HttpUtils.sendPostHeader(url,content,token);
        JSONObject jsonObject = JSONObject.parseObject(result);
        vo.setSuccess(jsonObject.getBoolean("success"));
        vo.setMessage(jsonObject.getString("message"));
        vo.setCode(jsonObject.getInteger("code"));
        if(vo.getCode() == 200){
            JSONObject resultJsonObject = jsonObject.getJSONObject("result");
            VeccReturnResultVO resultVo = new VeccReturnResultVO();
            resultVo.setState(resultJsonObject.getString("state"));
            resultVo.setDetail(resultJsonObject.getString("detail"));
            vo.setResultVO(resultVo);
        }
        VeccRequestVO requestVO = new VeccRequestVO();
        requestVO.setRequestUrl(url);
        requestVO.setContent(content);
        requestVO.setResult(result);
        vo.setRequestVO(requestVO);
        return vo;
    }

    public static void main(String[] args) {
        recordLogin("fw3umfi36a0o", "39bu9peu");
//        vehicleRecordAdd();
    }
}
